// 用 Promise 来通过异步处理方式来获取 XMLHttpRequest(XHR) 的数据

function getURL (URL) {
    return new Promise(function (resolve, reject) {
        var req = new XMLHttpRequest()
        req.open('GET', URL, true)
        req.onload = function () {
            if (req.status === 200) {
                resolve(req.responseText)
            } else {
                reject(new Error(req.statusText))
            }
        }
        req.onerror = function () {
            reject(new Error(req.statusText))
        }
        req.send()
    })
}

var URL = 'http://httpbin.org/get'

getURL(URL).then(function onFulfilled (value) {
    console.log(value);
}).catch(function onRejected (error) {
    console.log(error);
})

// 报错
var URL = "http://httpbin.org/status/500"; 
getURL(URL).then(function onFulfilled(value){
    console.log(value);
}).catch(function onRejected(error){ 
    console.error(error);
});